/*修改密码字段验证*/
$("#changePasswordModal").bootstrapValidator({
	message:"这是无效的数据域",
	feedbackIcons: {/*input状态样式图片*/
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields:{
    	changePassword: {
            message:'密码无效',
            validators: {
                notEmpty: {
                    message: '密码不能为空'
                },
                stringLength: {
                    min: 6,
                    max: 30,
                    message: '用户名长度必须在6到30之间'
                },
                identical: {//相同
                    field: 'changePassword', //需要进行比较的input name值
                    message: '两次密码不一致'
                },
            }
        },
        repChangePassword: {
            message: '密码无效',
            validators: {
                notEmpty: {
                    message: '密码不能为空'
                },
                stringLength: {
                    min: 6,
                    max: 30,
                    message: '密码长度必须在6到30之间'
                },
                identical: {//相同
                    field: 'changePassword',
                    message: '两次密码不一致'
                },
            }
        },
    }
});

/*修改密码*/
$("#changPasswordBtn").click(function(){
	$.ajax({
		url:"changePassword",
		data:{
			oldPassowrd:$("#oldPassword").val(),
			password:$("#changePassword").val(),
		},
		type:"POST",
		success:function(result){
			if(result.resultParm.code == "200"){
				alert(result.resultInfo);
			} else {
				alert(result.resultInfo);
				$("#changePasswordModal").modal("hide");
				alert("请重新登录!");
				window.location.href="index.html";
			}
		},
		error:function(){
			alert("修改密码失败");
		}
	});
});
/*预约表单验证*/
$("#game-form").bootstrapValidator({
	message:"这是无效的数据域",
	feedbackIcons: {/*input状态样式图片*/
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields:{
    	matchName:{
    		message:"无效",
    		validators:{
    			notEmpty:{
    				message:"赛事名称不可为空"
    			},
    			stringLength: {
                min: 3,
                max: 5,
                message: '赛事名称长度不正确'
                },
    		}
    	},
    	borrowAccount: {
            message:'无效',
            validators: {
                notEmpty: {
                    message: '用户账号不可为空'
                },
                stringLength: {
                min: 12,
                max: 12,
                message: '请输入正确的账号'
                },
            }
        },
        borrowName: {
            message:'无效',
            validators: {
                notEmpty: {
                    message: '用户名不能为空'
                },
                stringLength: {
                min: 2,
                max: 12,
                message: '请输入正确的用户名'
                },
            }
        },
        startTime: {
            message:'无效',
            validators: {
                notEmpty: {
                    message: '开始时间不能为空'
                },
            }
        },
        endTime: {
            message:'无效',
            validators: {
                notEmpty: {
                    message: '结束时间不能为空'
                },
            }
        },
        telephone: {
            message:'无效',
            validators: {
                notEmpty: {
                    message: '联系方式不能为空'
                },
                stringLength: {
                min: 11,
                max: 11,
                message: '请输入正确的手机号码'
                },
            }
        },
    }
});
/*获得当前时间*/
function p(s) {
    return s < 10 ? '0' + s: s;
};
var myDate = new Date();
//获取当前年
var year=myDate.getFullYear();
//获取当前月
var month=myDate.getMonth()+1;
//获取当前日
var date=myDate.getDate(); 
var h=myDate.getHours();       //获取当前小时数(0-23)
var m=myDate.getMinutes();     //获取当前分钟数(0-59)
var s=myDate.getSeconds();
var now=year+'-'+p(month)+"-"+p(date)+" "+p(h)+':'+p(m)+":"+p(s);
/*增加赛事*/
$("#addGame-btn").click(function(){
	$.ajax({
		type:"post",
		url:"addMatch",
		data:{
			matchName:$("#matchName").val(),
			borrowAccount:$("#borrowAccount").val(),
			borrowName:$("#borrowName").val(),
			startTime:$("#start").val(),
			endTime:$("#endTime").val(),
			telephone:$("#telephone").val(),
			placesId:$("#place-select").val(),
			equipId:$("#eq-select").val(),
			number:$("#number").val(),
		},
		success:function(result){
			alert(result.resultInfo);
			$(".game-table table tr td").remove();
			getAllMatch();
		},
		error:function(){
			alert("添加失败");
		}
	});
});
/*获得所有需要审核赛事*/
function getAllMatch(){
//	$(".game-table table tr td").remove();
	$.ajax({
		type:"get",
		url:"findAllMatchs",
		success:function(result){
			var info = result.resultParm.match;
//			console.log(info);
			$.each(info, function(index , item) {
//				console.log(item.status);
				if(item.status==0){
					var gameId = $("<td></td>").append(item.id);
					var gameName = $("<td></td>").append(item.matchName);
					var startTime = $("<td></td>").append(item.startTime);
					var endTime = $("<td></td>").append(item.endTime);
					var orderTime = $("<td></td>").append(item.orderTime);
					var placeA = $("<td></td>").append(item.places==null?"无":$("<a></a>").append("详情").attr("href","#placeDetails").attr("data-toggle","modal").attr("title","placeA"));
					var eqA = $("<td></td>").append(item.orders==null?"无":$("<a></a>").append("详情").attr("href","#eqDetails").attr("data-toggle","modal").attr("title","eqA"));
					var userA = $("<td></td>").append($("<a></a>").append("详情").attr("href","#userDetails").attr("data-toggle","modal").attr("title","userA"));
					var status = $("<td></td>").append($("<a></a>").append("审核通过").attr("href","#").attr("title","checkA")).append(" ").append(" ").append(" ").append($("<a></a>").append("审核不通过").attr("href","#").attr("title","noPassA"));
//					alert(gameId);
					$("<tr></tr>")
					.append(gameId)
					.append(gameName)
					.append(startTime)
					.append(endTime)
					.append(orderTime)
					.append(placeA)
					.append(eqA)
					.append(userA)
					.append(status)
					.appendTo(".game-table table");
				};
			});
			placeDetailsFunction();
			eqDetailsFunction();
			userDetailsFunction();
			pass();
			noPass();
		},
		error:function(){
			alert("错误");
		}
	});
};
/*进入页面能获得所有数据*/
getAllMatch();
/*获得场地详细信息*/
function placeDetailsFunction(){
	$("a[title=placeA]").click(function(){
		var num1 = $(this).parent().parent().children().eq(0).text();
		$.ajax({
			type:"get",
			url:"findAllMatchs",
			success:function(result){
				var info = result.resultParm.match;
				$.each(info, function(index , item){
					if(num1==item.id){
						var placeId = item.place.placeId;
						var placeName =item.place.placeName;
						var placeMoney =item.place.placeMoney;
						$("#placeId").val(placeId);
						$("#placeName").val(placeName);
						$("#placeMoney").val(placeMoney);
					};	
				});
			},
		});
	});
};
/*器材详情超链接*/
function eqDetailsFunction(){
	$("a[title=eqA]").click(function(){
		var num1 = $(this).parent().parent().children().eq(0).text();
		
		$.ajax({
			type:"get",
			url:"findAllMatchs",
			success:function(result){
				var info = result.resultParm.match;
				$.each(info, function(index , item){
//					console.log(item.equipment);
					if(num1==item.id){
						var eqId = item.equipment.id;
						var eqName =item.equipment.name;
						var eqMoney =item.equipment.remainNum;
						$("#eqId").val(eqId);
						$("#eqName").val(eqName);
						$("#eqNum").val(eqMoney);
					};	
				});
			},
		});
	});
};
/*用户详情超链接*/
function userDetailsFunction(){
	$("a[title=userA]").click(function(){
		var num1 = $(this).parent().parent().children().eq(0).text();
		$.ajax({
			type:"get",
			url:"findAllMatchs",
			success:function(result){
				var info = result.resultParm.match;
				$.each(info, function(index , item){
//					console.log(item.equipment);
					if(num1==item.id){
						var borrowAccount = item.borrowAccount;
						var borrowName =item.borrowName;
						var telephone =item.telephone;
						$("#uesrId").val(borrowAccount);
						$("#userName").val(borrowName);
						$("#tel").val(telephone);
					};	
				});
			},
		});
	});
}
/*审核通过按钮*/
function pass(){
	$("a[title=checkA]").click(function(){
		var num1 = $(this).parent().parent().children().eq(0).text();
		var r=confirm("确认通过审核么？");
		if(r==true){;
			$.ajax({
				type:"get",
				url:"updataMatch",
				data:{
					matchId:num1,
					type:1
				},
				success:function(result){
					alert(result.resultInfo);
					$(".game-table table tr td").remove();
					getAllMatch();
					
				},
			});	
		};
	});
};
/*审核不通过按钮*/
function noPass(){
	$("a[title=noPassA]").click(function(){
		var num1 = $(this).parent().parent().children().eq(0).text();
		var r=confirm("确认不通过审核么？");
		if(r==true){;
			$.ajax({
				type:"get",
				url:"updataMatch",
				data:{
					matchId:num1,
					type:2
				},
				success:function(result){
					alert(result.resultInfo);
					$(".game-table table tr td").remove();
					getAllMatch();
				},
			});	
		};
	});
};
/*查询按钮*/
$("#search-game").click(function(){
	$(".game-table table tr td").remove();
	$.ajax({
		type:"post",
		url:"findMatchByName",
		data:{
			name :$("#gameName").val(),
		},
		success:function(result){
			var item = result.resultParm.match;
//			console.log(info);
//				console.log(item.status);
				if(item.status==0){
					var gameId = $("<td></td>").append(item.id);
					var gameName = $("<td></td>").append(item.matchName);
					var startTime = $("<td></td>").append(item.startTime);
					var endTime = $("<td></td>").append(item.endTime);
					var orderTime = $("<td></td>").append(item.orderTime);
					var placeA = $("<td></td>").append(item.places==null?"无":$("<a></a>").append("详情").attr("href","#placeDetails").attr("data-toggle","modal").attr("title","placeA"));
					var eqA = $("<td></td>").append(item.orders==null?"无":$("<a></a>").append("详情").attr("href","#eqDetails").attr("data-toggle","modal").attr("title","eqA"));
					var userA = $("<td></td>").append($("<a></a>").append("详情").attr("href","#userDetails").attr("data-toggle","modal").attr("title","userA"));
					var status = $("<td></td>").append($("<a></a>").append("审核通过").attr("href","#").attr("title","checkA")).append(" ").append(" ").append(" ").append($("<a></a>").append("审核不通过").attr("href","#").attr("title","noPassA"));
//					alert(gameId);
					$("<tr></tr>")
					.append(gameId)
					.append(gameName)
					.append(startTime)
					.append(endTime)
					.append(orderTime)
					.append(placeA)
					.append(eqA)
					.append(userA)
					.append(status)
					.appendTo(".game-table table");
				};
			placeDetailsFunction();
			eqDetailsFunction();
			userDetailsFunction();
			pass();
			noPass();
		},
		error:function(){
			alert("查询失败");
		}
	});
});
$("#add-game").click(function(){
	$("#start").val(now);
});